package com.example.zhinongbao.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.example.zhinongbao.Model.UserManager;
import com.example.zhinongbao.R;
import com.example.zhinongbao.database.DBHelper;
import com.example.zhinongbao.entity.UserInfo;
import com.example.zhinongbao.view.MainActivity;
import com.example.zhinongbao.view.MineFragment;

public class LoginActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private CheckBox checkbox;
    private  boolean is_login;
    private SharedPreferences mSharedPreferences;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //获取mSharedPreferences
        mSharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        checkbox = findViewById(R.id.checkbox);
        //是否勾选了密码
        if(is_login){
            String username=mSharedPreferences.getString("username",null);
            String password=mSharedPreferences.getString("password",null);
        et_username.setText(username);
        et_password.setText(password);
        checkbox.setChecked(true);


        }
        //点击注册
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到注册页面
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
//登录
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString().trim();
                String password = et_password.getText().toString().trim();
//                String user_id = et_password.getText().toString().trim();
//                String phone = et_password.getText().toString().trim();
//                String age = et_password.getText().toString().trim();
//                String address = et_password.getText().toString().trim();
                 mSharedPreferences= getSharedPreferences("user", Context.MODE_PRIVATE);
                if (TextUtils.isEmpty(username) && TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this, "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                } else {

                    //实现登录
                    UserInfo login = DBHelper.getInstance(LoginActivity.this).login(username);
                    if(login!=null){
                        if(username.equals(login.getUsername())&&password.equals(login.getPassword())){
                            //保存是否勾选了记住密码框
                            SharedPreferences.Editor edit=mSharedPreferences.edit();
                             edit.putBoolean("is_login",is_login);
                             edit.putString("username",username);
                            edit.putString("password",password);
//                            edit.putString("user_id",user_id);
//                            edit.putString("phone",phone);
//                            edit.putString("age",age);
//                            edit.putString("address",address);
//                            edit.putString("paypassword",paypassword);
                            Log.d("sp", "save username = " + username);

                           // 这句话不能少
                              edit.commit();
//                            edit.apply();//使用apply异步保存，使用commit同步保存
                              //登陆成功
                            UserManager.setLoggedIn(true);
                            Intent intent =new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                        else{
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
else { Toast.makeText(LoginActivity.this, "账号未注册", Toast.LENGTH_SHORT).show();

                    }


                }
            }
        });
        //checkbox的点击事件
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_login=isChecked;
            }
        });
    }}

